#region Copyright Notice
/*
Copyright (C) 2011 by Ashley Ross

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion Copyright Notice

using System;
using System.Runtime.InteropServices;

namespace Manta.IO {
    /// <summary>
    /// Implements the ReparseGuidDataBuffer struct.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public class ReparseGuidDataBuffer : IReparseDataBuffer {
        // * Reparse point tag. This member identifies the structure of the user-defined reparse data. 
        private ReparseTag _reparseTag;
        // * Size of the reparse data in the DataBuffer member, in bytes. This value may vary with different tags and may vary between two uses of the same tag.
        private ushort _reparseDataLength;
        // * Reserved; do not use. :)
        private ushort _reserved;
        // * GUID that uniquely identifies the reparse point. 
        private Guid _reparseGuid;
        // User-defined data for the reparse point. The contents are determined by the reparse point implementer.
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaximumBufferSize)]
        private byte[] _dataBuffer;


        // Size, in bytes, of the fields marked *
        private const int HeaderSize = 24;
        /// <summary>
        /// Gets the maximum size, in bytes, of the DataBuffer
        /// </summary>
        public const int MaximumBufferSize = 0x3FE8;


        /// <summary>
        /// Initializes a new instance of the ReparseGuidDataBuffer class.
        /// </summary>
        public ReparseGuidDataBuffer() {
            _dataBuffer = new byte[MaximumBufferSize];
            _reparseDataLength = (ushort) MaximumBufferSize;
        }

        /// <summary>
        /// Initializes a new instance of the ReparseGuidDataBuffer class with the specified reparse tag.
        /// </summary>
        /// <param name="tag">The reparse tag to initialize the ReparseGuidDataBuffer with.</param>
        public ReparseGuidDataBuffer(ReparseTag tag)
            : this() {
            _reparseTag = tag;
        }

        /// <summary>
        /// Initializes a new instance of the ReparseGuidDataBuffer class with the specified reparse tag.
        /// </summary>
        /// <param name="tag">The reparse tag to initialize the ReparseGuidDataBuffer with.</param>
        public ReparseGuidDataBuffer(uint tag)
            : this((ReparseTag) tag) {
        }

        /// <summary>
        /// Initializes a new instance of the ReparseGuidDataBuffer class with the specified reparse tag and GUID.
        /// </summary>
        /// <param name="tag">The reparse tag to initialize the ReparseGuidDataBuffer with.</param>
        /// <param name="guid">The GUID to initialize the ReparseGuidDataBuffer with.</param>
        public ReparseGuidDataBuffer(ReparseTag tag, Guid guid)
            : this() {
            _reparseTag = tag;
            _reparseGuid = guid;
        }

        /// <summary>
        /// Initializes a new instance of the ReparseGuidDataBuffer class with the specified reparse tag and GUID.
        /// </summary>
        /// <param name="tag">The reparse tag to initialize the ReparseGuidDataBuffer with.</param>
        /// <param name="guid">The GUID to initialize the ReparseGuidDataBuffer with.</param>
        public ReparseGuidDataBuffer(uint tag, Guid guid)
            : this((ReparseTag) tag, guid) {
        }


        /// <summary>
        /// Gets or sets the reparse tag of the current ReparseGuidDataBuffer.
        /// </summary>
        public ReparseTag Tag {
            get { return _reparseTag; }
            set { _reparseTag = value; }
        }

        /// <summary>
        /// Gets or sets the GUID of the current ReparseGuidDataBuffer.
        /// </summary>
        public Guid ReparseGuid { 
            get { return _reparseGuid; }
            set { _reparseGuid = value; }
        }

        /// <summary>
        /// Gets or sets the data buffer of the current ReparseGuidDataBuffer.
        /// </summary>
        public byte[] DataBuffer {
            get { return _dataBuffer; }
            set {
                if (value != null && value.Length > MaximumBufferSize)
                    throw new ArgumentException("The length of the data buffer cannot be larger than MaximumBufferSize.");

                _dataBuffer = value;
                _reparseDataLength = value == null ? (ushort) 0 : (ushort) value.Length;
            }
        }

        /// <summary>
        /// Gets the size, in bytes, of the current ReparseGuidDataBuffer.
        /// </summary>
        public int Size {
            get { return HeaderSize + _reparseDataLength; }
        }
    }
}